//
//  AIGirlMsgLevelDetailView.swift
//  AIGirl
//
//  Created by Rae on 2024/1/30.
//

import UIKit

class AIGirlMsgLevelDetailView: UIView, AnimationProtocol, AnimationDelegate, UorInXNetworkProtocol {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 420.0)
    }
    
    func animationContainerViewStyle() -> AnimationViewStyle {
        .bottom
    }

    private lazy var bgImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Message_Level_bg").mode(.scaleAspectFill).end()
    private lazy var minImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private lazy var maxImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private lazy var proImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Message_Level_pro").end()
    private lazy var preImageView = c_make_ex(UIButton.self)
        .image("AIGirl_Message_Level_pre").end()
    private lazy var proBgView = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.2)).end()
    private lazy var levelLabel = c_make_ex(UILabel.self)
        .color(.white).alignment(.center).font(13.font_semiItalic).end()
    private lazy var desLabel = c_make_ex(UILabel.self)
        .color("#E2D2FC").text("“With increased closeness, her passion rises.”")
        .alignment(.center).font(12.font_medium).end()
    private lazy var bottomLabel = c_make_ex(UILabel.self)
        .color("#E2D2FC").text("status for double intimacy points.")
        .alignment(.center).font(16.font_medium).end()
    
    var model:AIGirlMessageLevelModel?{
        didSet{
            guard let md = model else { return }
            let min = "AIGirl_Message_Level_" + md.lastLevel
            let max = "AIGirl_Message_Level_" + md.nextLevel
            let currentPx = md.intimacy - md.gap
            minImageView.image = min.toImage
            maxImageView.image = max.toImage
            levelLabel.text = "\(currentPx)px/\(md.intimacy)px" 
            if md.intimacy != 0 {
                proImageView.widthConstraint?.constant = (CGFloat.screenWidth - 120.0) * (CGFloat(currentPx)/CGFloat(md.intimacy))
            }
            
            if md.lastLevel == "10" {
                levelLabel.text = "\(100)px/\(100)px"
                proImageView.widthConstraint?.constant = (CGFloat.screenWidth - 120.0)
                maxImageView.image = "AIGirl_Message_Level_10".toImage
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        loadData()
        subviews{
            bgImageView.subviews{
                proBgView.subviews{
                    proImageView
                }
                minImageView
                maxImageView
                preImageView
                desLabel
                bottomLabel
                levelLabel
            }
        }
        
        bgImageView.fillContainer()
        bottomLabel.bottom(42).centerHorizontally()
        preImageView.width(108).height(20).centerHorizontally()
        preImageView.Bottom == bottomLabel.Top - 5
        desLabel.centerHorizontally()
        desLabel.Bottom == preImageView.Top - 10
        proBgView.leading(60).trailing(60).height(18)
        proBgView.Bottom == desLabel.Top - 20
        proImageView.leading(0).width(0).top(0).bottom(0)
        minImageView.leading(30).size(60)
        minImageView.CenterY == proImageView.CenterY
        maxImageView.trailing(30).size(60)
        maxImageView.CenterY == proImageView.CenterY
        levelLabel.centerHorizontally()
        levelLabel.CenterY == proImageView.CenterY
        
        bgImageView.isUserInteractionEnabled = true
        preImageView.addTarget(self, action: #selector(clickPremiumBtn), for: .touchUpInside)
    }
    
    @objc private func clickPremiumBtn(){
        
        if GlobalTool.userModel?.isVip.intValue ?? 0 > 0 {
            GlobalTool.showAlreadyVipInfoPage()
        }else{
            GlobalTool.showVipInfoPage()
        }
    }
    
    private func loadData(){
        
        request.start(.getIntimacy,type: AIGirlMessageLevelModel.self, isLoading: false) { result in
            
            if result.isSuccessful, let model = result.model {
                self.model = model
                NotificationCenter.default.post(name: .updateLevel, object: model.lastLevel)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
